package com.itqhy.controller;

import com.itqhy.pojo.*;
import com.itqhy.service.ProductService;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource(name = "productService")
    private ProductService productService;
    /**
     * 通过ajax查询商品的信息
     */
    @RequestMapping("/ajaxSelect")
        public void findProductByAjaxAndProName(HttpServletResponse response, String name) throws IOException {
        PrintWriter out=response.getWriter();
        List<String> list=productService.findProductByProductName(name);
        String str=JSONArray.fromObject(list).toString();//此时str就是一个数组
        out.print(str);//将数组返回
    }

    /**
     * 分页显示商品信息
     */
    private int pageCode=1;
    private int pageSize=4;

    @RequestMapping("/pageListProduct")
    public String pageListProduct(PageBean pageBean, Model model,String name){
        if (name!=""){
            pageBean.setName(name);
        }

        if(pageBean.getPageCode()!=null){
            pageCode=pageBean.getPageCode();
             }
        pageBean.setPageCode(pageCode);
        pageBean.setPageSize(pageSize);
        pageBean.setStart((pageBean.getPageCode()-1) * pageBean.getPageSize());
        PageBean p=productService.pageListProduct(pageBean);
        model.addAttribute("p",p);
        return "/product_list.jsp";

    }
    /**
     * 根据id将商品信息查询出来
     */
    @RequestMapping("/findProductById")
    public String findProductById(String id,Model model){

        Product product=productService.findProductById(id);
        model.addAttribute("qhyProduct",product);
        return "/product_info.jsp";
    }
    /**
     * 将商品添加到购物车中
     */
    @RequestMapping("/addCar")
    public String addCar(String id, String count, HttpSession session) throws Exception {

        Product product=productService.findProductById(id);
        CartItem cartItem=new CartItem();
        cartItem.setCount(Integer.parseInt(count));
        cartItem.setProduct(product);
        Map<String,CartItem> map = (Map<String,CartItem>)session.getAttribute("car");
        Map<String,CartItem> qhyCar= Car.addCar(cartItem,map);
        session.setAttribute("car",qhyCar);
        return "/cart.jsp";
    }
    /**
     * 删除购物车中的商品
     */
    @RequestMapping("/deleteCar")
    public String deleteCarById(String id,HttpSession session){

        Map<String,CartItem> map = (Map<String,CartItem>)session.getAttribute("car");
        Set<String> keys=map.keySet();
       Iterator<String> in=keys.iterator();
       while (in.hasNext()){
           String key=in.next();
           if (key.equals(id)){
               in.remove();
           }
           session.setAttribute("car",map);
       }
        return "/cart.jsp";
    }
    /**
     * 查看购物车
     */
    @RequestMapping("/shopCar")
    public String lookCar(){
        return "/cart.jsp";
    }

}
